/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ta.entity.StDepartment;
import com.thinkgem.jeesite.modules.ta.service.StDepartmentService;

/**
 * 部门信息Controller
 * 
 * @author TTAO
 * @version 2017-05-31
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stDepartment")
public class StDepartmentController extends BaseController {

	@Autowired
	private StDepartmentService stDepartmentService;

	@ModelAttribute
	public StDepartment get(@RequestParam(required = false) String id) {
		StDepartment entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stDepartmentService.get(id);
		}
		if (entity == null) {
			entity = new StDepartment();
		}
		return entity;
	}

	@RequiresPermissions("ta:stDepartment:view")
	@RequestMapping(value = { "list", "" })
	public String list(StDepartment stDepartment, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<StDepartment> list = stDepartmentService.findList(stDepartment);
		model.addAttribute("list", list);
		return "modules/ta/stDepartmentList";
	}

	@RequiresPermissions("ta:stDepartment:view")
	@RequestMapping(value = "form")
	public String form(StDepartment stDepartment, Model model) {
		if (stDepartment.getParent() != null
				&& StringUtils.isNotBlank(stDepartment.getParent().getId())) {
			stDepartment.setParent(stDepartmentService.get(stDepartment
					.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(stDepartment.getId())) {
				StDepartment stDepartmentChild = new StDepartment();
				stDepartmentChild.setParent(new StDepartment(stDepartment
						.getParent().getId()));
				List<StDepartment> list = stDepartmentService
						.findList(stDepartment);
				if (list.size() > 0) {
					stDepartment.setSort(list.get(list.size() - 1).getSort());
					if (stDepartment.getSort() != null) {
						stDepartment.setSort(stDepartment.getSort() + 30);
					}
				}
			}
		}
		if (stDepartment.getSort() == null) {
			stDepartment.setSort(30);
		}
		model.addAttribute("stDepartment", stDepartment);
		return "modules/ta/stDepartmentForm";
	}

	@RequiresPermissions("ta:stDepartment:edit")
	@RequestMapping(value = "save")
	public String save(StDepartment stDepartment, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stDepartment)) {
			return form(stDepartment, model);
		}
		stDepartmentService.save(stDepartment);
		addMessage(redirectAttributes, "保存部门信息成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stDepartment/?repage";
	}

	@RequiresPermissions("ta:stDepartment:edit")
	@RequestMapping(value = "delete")
	public String delete(StDepartment stDepartment,
			RedirectAttributes redirectAttributes) {
		stDepartmentService.delete(stDepartment);
		addMessage(redirectAttributes, "删除部门信息成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stDepartment/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(
			@RequestParam(required = false) String extId,
			HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<StDepartment> list = stDepartmentService
				.findList(new StDepartment());
		for (int i = 0; i < list.size(); i++) {
			StDepartment e = list.get(i);
			if (StringUtils.isBlank(extId)
					|| (extId != null && !extId.equals(e.getId()) && e
							.getParentIds().indexOf("," + extId + ",") == -1)) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getDepartid());
				map.put("pId", e.getParentId());
				map.put("name", e.getDepartName());
				mapList.add(map);
			}
		}
		return mapList;
	}

}